package com.ht.controller;


import cn.hutool.json.JSONUtil;
import com.ht.data.Result;
import com.ht.dto.CureDiary;
import com.ht.dto.User;
import com.ht.service.ICureDiaryService;
import com.ht.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 代齐园
 * @since 2022-05-28
 */
@RestController
@RequestMapping("/cureDiary")
@Api(value = "看病日记")
public class CureDiaryController {
    @Autowired
    private ICureDiaryService cureDiaryService;

    @PostMapping("/insertDiary")
    //value为这个方法干什么用的
    @ApiOperation(value = "用药日记")
    //name的意思是传的参是谁，value的意思是传的是什么，required是否必填，paramType传参的方式
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户token", required = true, paramType = "header"),
            @ApiImplicitParam(name = "cureDiary", value = "用户看病日记增加的数据", required = true, paramType = "body")
    })
    public Result insertDiary(@RequestHeader("AuthToken") String token, @RequestBody CureDiary cureDiary) {
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        cureDiaryService.insertDiary(user.getId(), cureDiary);
        return Result.OK().setData("添加成功");
    }

    @GetMapping("/getMyDiary")
    @ApiOperation(value = "查询和用户相关的看病日记")
    @ApiImplicitParam(name = "token", value = "用户token", required = true, paramType = "header")
    public Result getMyDiary(@RequestHeader("AuthToken") String token) {
        Claims claims = new JwtUtil().parseJwt(token);
        User user = JSONUtil.toBean(claims.getSubject(), User.class);
        List<CureDiary> doctorList = cureDiaryService.getMyDiary(user.getId());
        return Result.OK().setData(doctorList);
    }
}
